package com.shopping.controller;

import com.shopping.bean.Collect;
import com.shopping.bean.User;
import com.shopping.service.impl.CollectService;
import com.shopping.util.LayData;
import com.shopping.util.SYS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("collect")
public class CollectController {
    @Autowired
    CollectService collectService;

    //添加收藏商品信息
    @RequestMapping("add")
    @ResponseBody
    public Map<String, Object> addCollect(@RequestParam("productid") int productid,HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        User loginUser=(User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        int userid=loginUser.getUserid();
        if (userid == 0) {
            result.put("code", 401);
            result.put("msg", "请先登录");
            return result;
        }

        try {
            if (collectService.addCollect(userid, productid)) {
                result.put("code", 0);
                result.put("msg", "收藏成功");
            } else {
                result.put("code", 1);
                result.put("msg", "该商品已收藏，不能重复收藏");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "收藏失败: " + e.getMessage());
        }
        return result;
    }

    @RequestMapping("list")
    @ResponseBody
    public Map<String, Object> getCollectListByUserId(HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        User loginUser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if (loginUser == null) {
            result.put("code", 401);
            result.put("msg", "用户未登录");
            return result;
        }
        int userId = loginUser.getUserid();
        try {
            List<Collect> collectList = collectService.getCollectListByUserId(userId);
            result.put("code", 200);
            result.put("data", collectList);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败：" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Map<String, Object> deleteCollect( @RequestParam("productId") int productId,HttpSession session) {
        Map<String, Object> result = new HashMap<>();
        User loginUser = (User) session.getAttribute(SYS.SHOPPING_LOGIN_USER);
        if (loginUser == null) {
            result.put("code", 403);
            result.put("msg", "用户未登录");
            return result;
        }
        int userId = loginUser.getUserid();
        try {
            boolean success = collectService.deleteCollect(userId, productId);
            if (success) {
                result.put("code", 200);
                result.put("msg", "取消收藏成功");
            } else {
                result.put("code", 500);
                result.put("msg", "取消收藏失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "取消收藏失败：" + e.getMessage());
        }
        return result;
    }
    @RequestMapping("deleteById")
    @ResponseBody
    public Map<String, Object> deleteCollectById(@RequestParam("collectId") int collectid) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = collectService.deleteCollectById(collectid);
            if (success) {
                result.put("code", 200);
                result.put("msg", "取消收藏成功");
            } else {
                result.put("code", 500);
                result.put("msg", "取消收藏失败");
            }
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "取消收藏失败：" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("count")
    @ResponseBody
    public Map<String, Object> getCollectCountByUserId(@RequestParam("userId") int userId) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = collectService.getCollectCountByUserId(userId);
            result.put("code", 200);
            result.put("count", count);
        } catch (Exception e) {
            result.put("code", 500);
            result.put("msg", "查询失败：" + e.getMessage());
        }
        return result;
    }



}
